function preStrForSearch(str) {
	var r = str.replace(/\|/g, " ");
	//remove the unused string
	while(r.indexOf("  ") > 0)
		r = r.replace(/\s\s/g, " ");
	return r.replace(/\s/g, "|");
}

function drawInUnit() {
	var ddl = ddlInUnit;
	ddl.children().remove();
	if (inUnitUsers != null && inUnitUsers.length > 0) {
		for(var i = 0; i < inUnitUsers.length; i++) {
			var id = inUnitUsers[i].id;
			var name = inUnitUsers[i].name;
			jQuery("<option value='"+ id+ "'>"+ name+ " ("+ id+ ")</option>").appendTo(ddl);
		}
	}
}

function drawOther(userArr) {
	var ddl = ddlOther;
	ddl.children().remove();
	
	if(userArr == null || userArr.length < 1)
		jQuery("<option>No User Found</option>").appendTo(ddl);
	else
		for(var i = 0; i < userArr.length; i++) {
			var id = userArr[i].id;
			var name = userArr[i].name;
			jQuery("<option value='"+ id+ "'>"+ name+ " ("+ id+ ")</option>").appendTo(ddl);
		}
}

//used in all function
var ddlOther, ddlInUnit, submitResult;

jQuery(document).ready(function() {
	//setup variable
	ddlOther = jQuery("#ddlOther");
	ddlInUnit = jQuery("#ddlInUnit");
	submitResult = jQuery("#submitResult");
	//setup display
	drawInUnit();
	submitResult.hide();
	
	//Search function
	jQuery("#search_user_form").submit(function(event) {
		event.preventDefault();
		
		var key = jQuery("#txtKey").val();
		var typeSel = jQuery("#rdStudent").attr("checked");
		var type = typeSel ? "student" : "staff";
		key = preStrForSearch(key);
		
		jQuery.ajax({
			type: "POST",
			url: "f_search_user.jsp",
			data: {
				user_type : type,
				name_list : key
			},
			datatype: "json",
			success: drawOther
		});
	});
	//Add all
	jQuery("#btnAddAll").click(function(event) {
		var selected = ddlOther.children();
		selected.each(function() {
			var selectOpt = jQuery(this);
			var val = selectOpt.val();
			if(ddlInUnit.children("option[value='"+ val+ "']").length < 1)
				selectOpt.appendTo(ddlInUnit);
			else
				selectOpt.remove();
		});
	});
	//Add selected
	jQuery("#btnAddSelect").click(function(event) {
		var selected = ddlOther.children("option:selected");
		selected.each(function() {
			var selectOpt = jQuery(this);
			var val = selectOpt.val();
			if(ddlInUnit.children("option[value='"+ val+ "']").length < 1)
				selectOpt.appendTo(ddlInUnit);
			else
				selectOpt.remove();
		});
	});
	//Del all
	jQuery("#btnDelAll").click(function(event) {
		var selected = ddlInUnit.children();
		selected.each(function() {
			var selectOpt = jQuery(this);
			var val = selectOpt.val();
			if(ddlOther.children("option[value='"+ val+ "']").length < 1)
				selectOpt.appendTo(ddlOther);
			else
				selectOpt.remove();
		});
	});
	//del selected
	jQuery("#btnDelSelect").click(function(event) {
		var selected = ddlInUnit.children("option:selected");
		selected.each(function() {
			var selectOpt = jQuery(this);
			var val = selectOpt.val();
			if(ddlOther.children("option[value='"+ val+ "']").length < 1)
				selectOpt.appendTo(ddlOther);
			else
				selectOpt.remove();
		});
	});
	//undo all
	jQuery("#btnUndoAll").click(drawInUnit);
	//Submit
	jQuery("#btnSubmit").click(function() {
		var eles = ddlInUnit.children();
		var listUser = new Array();
		eles.each(function() {
			var id = jQuery(this).val();
			listUser.push(id);
		});
		
		var listStr = jQuery.toJSON(listUser);
		submitResult.hide();
		
		//submit info for update
		jQuery.ajax({
			type: "POST",
			url: "f_confirm_user_unit.jsp",
			data: {
				unit_id : selectUnit,
				name_list : listStr
			},
			datatype: "json",
			success: function(data) {
				submitResult.show();
				if(data.isError) {
					//error respone
					submitResult.addClass("err");
				} else {
					submitResult.removeClass();
				}
				submitResult.text(data.Msg);
			}
		});
	});
});